package com.tengju.data.infrastructure.schedule;


import com.tengju.data.domain.model.alert.AlertEvent;
import com.tengju.data.domain.model.alert.AlertService;
import com.tengju.data.domain.model.report.AchievementBonusDateConfig;
import com.tengju.data.domain.model.service.AchievementBonusDateConfigService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.HashSet;
import java.util.Set;

/**
 * @author kezunfa
 * 市场补贴日期相关配置表异常数据监控
 */
@Component
@Slf4j
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class BonusDateConfigMonitorJob {

    private final AlertService alertService;
    private final AchievementBonusDateConfigService bonusDateConfigService;

    private final String jobName = "业绩奖励日期配置校验任务";

    /**
     * 每次校验未来最近15天的配置是否完整，是否完善
     * @param params
     * @return
     */
    @XxlJob("bonusDateConfigMonitorJob")
    public ReturnT<String> execute(String params) {
        try {
            LocalDate now = LocalDate.now();
            Set<AchievementBonusDateConfig> checkSet = new HashSet<>();
            //手动输入传参
            if (!StringUtils.isBlank(params)) {
                LocalDate checkDate = LocalDate.parse(params);
                AchievementBonusDateConfig dateConfig = bonusDateConfigService.queryByCalDate(checkDate);
                doCheck(checkDate,dateConfig,checkSet);
                return ReturnT.SUCCESS;
            }
            for (int i = 0; i < 15; i++) {
                LocalDate checkDate = now.plusDays(i);
                AchievementBonusDateConfig dateConfig = bonusDateConfigService.queryByCalDate(checkDate);
                doCheck(checkDate, dateConfig, checkSet);
            }
        } catch (Exception e) {
            log.error("{}, jobError",jobName, e);
            AlertEvent event = new AlertEvent(jobName + "异常");
            event.addItem("任务参数",params);
            event.addItem("异常信息",e.getMessage());
            alertService.alert(event);
        }
        return ReturnT.SUCCESS;
    }

    private void doCheck(LocalDate checkDate, AchievementBonusDateConfig dateConfig, Set<AchievementBonusDateConfig> checkSet) {
        if (dateConfig == null) {
            AlertEvent event = new AlertEvent(jobName + "异常");
            event.addItem("checkDate",checkDate);
            event.addItem("异常","当天配置不存在");
            alertService.alert(event);
            return;
        }
        if (checkSet.contains(dateConfig)) {
            return;
        }
        checkSet.add(dateConfig);
        if (dateConfig.isComplete() && dateConfig.isValid()) {
            return;
        }
        AlertEvent event = new AlertEvent(jobName + "异常");
        event.addItem("checkDate",checkDate);
        event.addItem("config",dateConfig);
        event.addItem("异常","当天配置不完整，或者日期配置不合法");
        alertService.alert(event);
    }


}
